package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Student;
import service.StudentService;
import utils.Page;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/list/{currentPage}")
    public String list(Student student, @PathVariable("currentPage")Integer currentPage, Model model){
        Page page=new Page();
        int totalCount = studentService.count(student);
        page.setTotalCount(totalCount);
        page.setCurrentPage(currentPage);
        page.setTotalPage();

        //计算偏移量
        int offset= (currentPage-1)*page.getPageSize();
        //全查询
        List<Student> students = studentService.findAll(offset,page.getPageSize());

        //List<Student> students2 = studentService.findStudentById(offset,page.getPageSize());

        model.addAttribute("students",students);
        model.addAttribute("page",page);
        model.addAttribute("student",student);

        return "student/list";
    }
@RequestMapping("/listName")
public String listName(@RequestParam("studentName")String studentName, Model model){
        List<Student> students = studentService.findStudentByName(studentName);
        model.addAttribute("students",students);
        model.addAttribute("studentName",studentName);
        return "student/list";
}

@RequestMapping("/listMajor")
public String listMajor(@RequestParam("major")String major, Model model){
    List<Student> students = studentService.findStudentByMajor(major);
    model.addAttribute("students",students);
    model.addAttribute("major",major);
    return "student/list";
}
@RequestMapping("/listGender")
public String listGender(@RequestParam("gender")String gender, Model model){
    List<Student> students = studentService.findStudentByGender(gender);
    model.addAttribute("students",students);
    model.addAttribute("gender",gender);
    return "student/list";
}
@RequestMapping("/listAge")
public String listAge(@RequestParam("age")Integer age, Model model){
    List<Student> students = studentService.findStudentByAge(age);
    model.addAttribute("students",students);
    model.addAttribute("age",age);
    return "student/list";
}
@RequestMapping("/listStatus")
public String listStatus(@RequestParam("status")String status, Model model){
    List<Student> students = studentService.findStudentByStatus(status);
    model.addAttribute("students",students);
    model.addAttribute("status",status);
    return "student/list";
}
@RequestMapping("/listNum")
public String listNum(@RequestParam("studentNum")String studentNum, Model model){
    List<Student> students = studentService.findStudentByNum(studentNum);
    model.addAttribute("students",students);
    model.addAttribute("studentNum",studentNum);
    return "student/list";
}
    @RequestMapping("/toadd")
    public String toAdd(){

        return "student/add";
    }

    @RequestMapping("/addSave")
    public String addSave(Student student){
        int result=studentService.addStudent(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else {
            return "error";
        }
    }

    @RequestMapping("/delete/{sid}")
    public String deleteById(@PathVariable("sid")Integer sid){
        int result = studentService.deleteStudentById(sid);
        if(result>0){
            return "redirect:/student/list/1";
        }else{
            return "error";
        }
    }

    @RequestMapping("/toupdate/{sid}")
    public String toupdate(@PathVariable("sid")Integer sid,Model model){
        Student student= studentService.findStudentById(sid);
        model.addAttribute("student",student);
        return "student/update";
    }

    @RequestMapping("/updateSave")
    public String updateSave(Student student){
        int result=studentService.updateStudent(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else{
            return "error";
        }
    }
}
